import axios from 'axios'
import { message } from 'antd'
import Cookie from 'js-cookie'

const request = axios.create({
    timeout: 3000,
    timeoutErrorMessage: '接口超时，请稍后再试'
})

//请求拦截
request.interceptors.request.use((config: any) => {
    //发送authorization
    const authorization = Cookie.get('token') as string;
    config.headers = {
        ...config.headers,
        authorization
    }
    return config

}, (err) => {
    return Promise.reject(err)
})

//相应拦截
request.interceptors.response.use((config) => {
    return config.data
}, (err) => {
    message.error(err.response.data.msg || '接口发生错误，请稍后再试')

    return Promise.reject(err)
})

export default request